#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
__author__ : xyh
__date__: 2016-09-14
新三板公告爬虫(源:巨潮网),每日增量更新
"""

import os
import sys
from ConfigParser import ConfigParser
from datetime import datetime

from scpy.logger import get_logger
from xtls.timeparser import parse_time
from crawler.artificial_xinsanban_anno import XsbCrawler
from queue.anno_producer import BuildXsb
from queue.anno_producer import produce_message

reload(sys)
sys.setdefaultencoding("utf-8")
logger = get_logger(__file__)

amqp_url = 'amqp://sc-admin:1qaz2wsx@10.51.29.242:5672/%2F?connection_attempts=3&heartbeat_interval=3600'


def crawler():
    CATEGORY = {
        "category_cxpl_gfzr": u"持续信息披露",
        "category_dqgg_gfzr": u"定期公告",
        "category_lsgg_gfzr": u"临时公告",
        "category_scpl_gfzr": u"首次信息披露",
        "category_zjjg_gfzr": u"中介机构公告",

        "category_cxpl_lwts": u"持续信息披露",
        "category_dqgg_lwts": u"定期公告",
        "category_lsgg_lwts": u"临时公告",
        "category_scpl_lwts": u"首次信息披露",
        "category_zjjg_lwts": u"中介机构公告",
    }

    for category in CATEGORY:
        logger.info('crawling %s-%s' % (category, CATEGORY[category]))
        XsbCrawler(category).run()


def publish_message(STARTUP_TIME):
    config = ConfigParser()
    config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'config.cfg'))
    with open(config_path, 'rb') as cfgfile:
        config.readfp(cfgfile)

    amqp_url = 'amqp://sc-admin:1qaz2wsx@10.51.29.242:5672/%2F?connection_attempts=3&heartbeat_interval=3600'
    queue_name = config.get('queue', 'daily_queue_name')
    start_anno_time = parse_time(u'1天前')
    stop_anno_time = parse_time(u'1秒前')
    build_message = BuildXsb(start_anno_time=start_anno_time, stop_anno_time=stop_anno_time,
                                STARTUP_TIME=STARTUP_TIME)

    produce_message(amqp_url, queue_name, build_message)


def main():
    STARTUP_TIME = datetime.today()  # 程序启动时间 eg: datetime.datetime(2016, 9, 14, 13, 53, 4)
    crawler()
    publish_message(STARTUP_TIME)


if __name__ == '__main__':
    main()


